package nonDictionaryTokenAnalyzer;

import java.util.ArrayList;

import inporter.DictionaryImporter;
import inporter.TokenInporter;

public class SpitTokenProcessor 
{
	
	/*
	 * Function: Main function of splitting process, call [strSplit] function recursively to get to result
	 * Input: pToken -> Current token which needs to be split
	 * Output: strRtn -> The result of processor
	 */
	public String getProcessorResult(String pToken)
	{
		String strRtn = "";
		
		strRtn = strSplit(pToken, 1);
		
		return strRtn;
	}
	
	/*
	 * Function: this is the recursive function for splitting token
	 * Input: pCntToken -> Current token (part of token) which needs to be split
	 * 		  pSplitLoc -> Split location for current token
	 * Output: strRtn -> Result
	 */
	private String strSplit(String pCntToken, int pSplitLoc)
	{
		String strRtn = "";
		
		if (pCntToken.length() == 2)
		{
			return strReferenceScanner(pCntToken);
		}
		
		String strTokenLeft = pCntToken.substring(0, pSplitLoc);
		String strTokenRight = pCntToken.substring(pSplitLoc);
		
		//Check left part length first
		if (strTokenLeft.length() != 1)
		{
			//left part is longer than 1, so check reference
			String strTmpLeft = strReferenceScanner(strTokenLeft);
			String strTmpRight = "";
			
			if (strTmpLeft.equals(""))
			{
				//No result found from reference, so need further split
				if (pSplitLoc < pCntToken.length() - 1)
				{
					strRtn = strSplit(pCntToken, pSplitLoc + 1);
				}
				else
				{
					//if no split point for further split in the token, return "" back
					return "";
				}
			}
			else
			{
				//result found from reference, so the right part need to be deal with
				if (strTokenRight.length() > 1)
				{
					strTmpRight = strReferenceScanner(strTokenRight);
				}
				else
				{
					strTmpRight = strTokenRight;
				}
				
				if (strTmpRight.equals(""))
				{
					//No result found from reference, so need further split
					if (2 < strTokenRight.length() - 1)
					{
						strRtn = strSplit(strTokenRight, 2);
					}
					else
					{
						//if no split point for further split in the token, return "" back
						return "";
					}
				}
				else
				{
					//Result was found for both left and right, so return the result
					strRtn = strTmpLeft + ", " + strTmpRight;
					return strRtn;
				}
			}
		}
		else
		{
			//The length of left part is 1, so abandon left part, and start to handle right part
			String strTmpLeft = strTokenLeft;
			String strTmpRight = "";
			
			if (strTokenRight.length() > 1)
			{
				strTmpRight = strSplit(strTokenRight, 2);
			}
			else
			{
				strTmpRight = strTokenRight;
			}
			
			if (strTmpRight.equals(""))
			{
				//No result found from reference, so need further split
				if (2 < strTokenRight.length() - 1)
				{
					strRtn = strSplit(strTokenRight, 3);
				}
				else
				{
					//if no split point for further split in the right part of token, return "" back
					return "";
				}
			}
			else
			{
				//Result was found for both left and right, so return the result
				strRtn = strTmpLeft + ", " + strTmpRight;
				return strRtn;
			}
			
			//No result found from reference, so need further split
			if (pSplitLoc < pCntToken.length() - 1)
			{
				strRtn = strSplit(pCntToken, pSplitLoc + 1);
			}
			else
			{
				//if no split point for further split in the token, return "" back
				return "";
			}
			
		}		
		return strRtn;
	}
	
	/*
	 * Function: Scan the references to check current token is dictionary word or not
	 * Input: pCntToken -> Current Token
	 * Output: strRtn -> Result
	 */
	private String strReferenceScanner(String pCntToken)
	{
		String strRtn = "";
		
		//get tokens from files
		TokenInporter clsTInporter = new TokenInporter();
		clsTInporter.arlGetFileLines("input\\source.txt");
		ArrayList<String> arlFileTokens = clsTInporter.getFileToken();
		//get tokens from dictionary
		DictionaryImporter clsDInporter = new DictionaryImporter("dictionary");
		ArrayList<String> arlDicTokens = clsDInporter.getDicTokenArray();
		
		//Check abbreviation result first
		//Check tokens in files
		if (arlFileTokens.contains(pCntToken))
		{
			strRtn = arlFileTokens.get(arlFileTokens.indexOf(pCntToken));
			return strRtn;
		}
		//Check dictionary at last
		if (arlDicTokens.contains(pCntToken))
		{
			strRtn = arlDicTokens.get(arlDicTokens.indexOf(pCntToken));
			return strRtn;
		}
		
		return strRtn;
	}
	
	
}
